//
//  HMNewFeatureView.swift
//  SinaWeiBo
//
//  Created by immncjykeny on 2017/5/9.
//  Copyright © 2017年 immncjykeny. All rights reserved.
//

import UIKit
//新特性视图
class HMNewFeatureView: UIView {

    @IBOutlet weak var scrollView: UIScrollView!
    
   
    @IBOutlet weak var pageControl: UIPageControl!
    
    
    @IBOutlet weak var enterButton: UIButton!
    
    //进入微博
    @IBAction func enterStatus(_ sender: Any) {
        //进去之后就隐藏
        removeFromSuperview()
        
        
    }
    class func newFeatureView() -> HMNewFeatureView{
        let nib = UINib(nibName: "HMNewFeatureView", bundle: nil)
        
        let v = nib.instantiate(withOwner: nil
            , options: nil)[0] as! HMNewFeatureView
        
        v.frame = UIScreen.main.bounds
        
        return v
    }
    
   
    //加载新特性视图
    override func awakeFromNib() {
        
        let rect = UIScreen.main.bounds
        
        let count = 4
        
        for i in 0..<count {
            let imageName = "new_feature_\(i + 1)"
            
            let iv = UIImageView(image: UIImage(named: imageName))
            
            iv.frame = rect.offsetBy(dx: CGFloat(i) * rect.width, dy: 0)
            
             scrollView.addSubview(iv)
            }
        //设置滚动范围
        scrollView.contentSize = CGSize(width: CGFloat(count + 1) * rect.width, height: rect.height)
        //设置分页
        scrollView.isPagingEnabled = true
        //关闭弹簧
        scrollView.bounces = false
        
        //设置代理
        scrollView.delegate = self
        //关闭横向和纵向的进度条
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        
        //设置button隐身
        enterButton.isHidden = true
       
        
    }
}
extension HMNewFeatureView:UIScrollViewDelegate{
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        //1.判断是否是最后一个
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        
        if page == scrollView.subviews.count{
            
            removeFromSuperview()
        }
        //如果是倒数第二个 显示按钮
        enterButton.isHidden = (page != scrollView.subviews.count - 1)
        
    }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        //1.一旦滚动就隐藏
        enterButton.isHidden = true
        
        //2.计算当前偏移量
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width + 0.5)
        
        //3.设置分页控件属性
        pageControl.currentPage = page
        
        //4.设置分页控件隐藏
        pageControl.isHidden = (page == scrollView.subviews.count)
    }
}
